import { login, profile } from '@/api/user'
import { getToken, setToken } from '@/utils/auth'
import { resetRouter } from '@/router/index'
export default {
  namespaced: true,
  state: {
    info: {},
    token: getToken() || ''
  },
  mutations: {
    getInfo(state, newInfo) {
      state.info = newInfo
    },
    setToken(state, newToken) {
      state.token = newToken
      setToken(newToken)
    }
  },
  actions: {
    logout(context) {
      context.commit('getInfo', {})
      context.commit('setToken', '')
      resetRouter()
    },
    async setProfile(context) {
      const res = await profile()

      context.commit('getInfo', res.data)
      return res.data
    },
    async login(context, data) {
      //
      const res = await login(data)
      const token = res.data
      context.commit('setToken', token)
    }
  }
}
